vim.api.nvim_create_augroup('MyGroup', {})

vim.api.nvim_create_autocmd({ 'TextYankPost' }, {
    group = 'MyGroup',
    callback = function(_)
        vim.highlight.on_yank({ higroup = 'Visual', timeout = 200 })
    end
})

local have_not_checked = true
local autocmd_check_fsize = function(data)
    local f = data.file
    local size = vim.fn.getfsize(f)
    if size >= 5 * 1024 * 1024 then
        vim.ui.select({ 'Do not open', 'Go ahead' }, {
            prompt = 'File "' .. vim.fs.basename(f) .. '" is too big, is it ok to open it?'
        }, function(_, i)
            if i == 2 then
                vim.cmd('filetype plugin indent off')
                vim.cmd('syntax off')
                vim.cmd('noswapfile')
            else
                vim.api.nvim_buf_delete(data.buf, {})
            end
        end)
    end
    have_not_checked = false
end
vim.api.nvim_create_autocmd({ 'BufAdd' }, {
    group = 'MyGroup', callback = autocmd_check_fsize
})
vim.api.nvim_create_autocmd({ 'BufReadPre' }, {
    group = 'MyGroup',
    callback = function(data)
        if have_not_checked then
            autocmd_check_fsize(data)
            have_not_checked = true
        end
    end
})
